package com.k.base.jdk.hashcode;

/**
 * @author kivil
 */
public class Student {
    private String id;
    private String name;
    private Integer age;
    private String clz;

    public Student(String id, String name, int age, String clz) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
        this.clz = clz;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public String getClz() {
        return clz;
    }
    public void setClz(String clz) {
        this.clz = clz;
    }

    /**
     * 规范
     * (1)当obj1.equals(obj2)为true时，obj1.hashCode() == obj2.hashCode()必须为true
     * (2)当obj1.hashCode() == obj2.hashCode()为false时，obj1.equals(obj2)必须为false
     * 重写 equals了。。。。按照规范必须重写hashcode,保证equals返回true,hashcode也返回true
     * 如果不按照规范，不重写hashcode,则，equals返回true，hashcode不能保证同样返回true
     * @param obj obj
     * @return boolean
     */
    @Override
    public boolean equals(Object obj) {
        if(this == obj){
            return true;
        }
        if (obj instanceof Student) {
            Student that = (Student)obj;
            if(this.getId().equals(that.getId())) {
                return true;
            }
        }
        return false;
    }
    @Override
    public int hashCode() {
        int result = 17;
        result = 31 * result * id.hashCode();
        return result;
    }
    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", age=" + age + ", clz=" + clz + "]";
    }
}
